//
//  UIImage+Video.m
//  privacySecretary
//
//  Created by gui wei on 12-4-1.
//  Copyright (c) 2012年 shanghai enjoymedia. All rights reserved.
//

#import "UIImage+Video.h"

@implementation UIImage (VideoInfo)

- (UIImage *)addVideoInfo:(ALAsset*)curALAsset
{
	CGSize size = CGSizeMake(self.size.width, self.size.height);
    NSInteger imageScale = self.size.width / 75;
	
	UIGraphicsBeginImageContext(size);
	
	[self drawAtPoint:CGPointZero];
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, self.size.height, self.size.width, 0), self.CGImage);
    

    CGContextSetGrayFillColor(context, 0.0, 0.5);
	CGContextFillRect(context, CGRectMake(0, 57*imageScale, 75*imageScale, 18*imageScale));
    
    //添加图像
    UIImage* videoImage = [UIImage imageNamed:@"videoTag.png"];
    CGContextDrawImage(context, CGRectMake(6*imageScale, 61.5*imageScale, 16*imageScale, 9*imageScale), videoImage.CGImage);
    
    //添加字体
    NSString* videoDur = [curALAsset valueForProperty:ALAssetPropertyDuration];
    NSTimeInterval videoTime = [videoDur doubleValue];
    if(videoTime >= 60)
    {
        NSInteger min = videoTime/ 60;
        NSInteger sec = videoTime - min * 60;
        videoDur = [NSString stringWithFormat:@"%d:%02d  ", min, sec];
    }
    else
    {
        videoDur = [NSString stringWithFormat:@"0:%02d  ", (NSInteger)videoTime];
    }
    
    [[UIColor whiteColor] set];
    

    [videoDur drawInRect:CGRectMake(0, 59*imageScale, 75*imageScale, 18*imageScale) withFont:[UIFont boldSystemFontOfSize:12.0*imageScale] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
	
	UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();

	
	return result;
}

- (UIImage *)addVideoInfo_Camera:(NSTimeInterval)videoTime
{
    CGSize size = CGSizeMake(self.size.width, self.size.height);
    NSInteger imageScale = self.size.width / 75;
	
	UIGraphicsBeginImageContext(size);
	
	[self drawAtPoint:CGPointZero];
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, self.size.height, self.size.width, 0), self.CGImage);
    
    
    CGContextSetGrayFillColor(context, 0.0, 0.5);
	CGContextFillRect(context, CGRectMake(0, 57*imageScale, 75*imageScale, 18*imageScale));
    
    //添加图像
    UIImage* videoImage = [UIImage imageNamed:@"videoTag.png"];
    CGContextDrawImage(context, CGRectMake(6*imageScale, 61.5*imageScale, 16*imageScale, 9*imageScale), videoImage.CGImage);
    
    NSString* videoDur = [NSString string];
    if(videoTime >= 60)
    {
        NSInteger min = videoTime/ 60;
        NSInteger sec = videoTime - min * 60;
        videoDur = [NSString stringWithFormat:@"%d:%02d  ", min, sec];
    }
    else
    {
        videoDur = [NSString stringWithFormat:@"0:%02d  ", (NSInteger)videoTime];
    }
    
    [[UIColor whiteColor] set];
    
    
    [videoDur drawInRect:CGRectMake(0, 59*imageScale, 75*imageScale, 18*imageScale) withFont:[UIFont boldSystemFontOfSize:12.0*imageScale] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentRight];
	
	UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
    
	
	return result;
}


@end
